package com.jz.netthings.application.providerCollection.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jz.netthings.application.providerCollection.bean.ProviderCollVideoPath;
import com.jz.netthings.application.providerCollection.bean.ProviderCollection;
import com.jz.netthings.application.providerCollection.service.ProviderCollVideoPathService;
import com.jz.netthings.application.providerCollection.service.ProviderCollectionService;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.system.utils.AdscmSystemUtils;
import com.jz.netthings.utils.AdscmBaseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 查看采浆视频的前端控制器
 */
@RequestMapping("/adscm/application/providerCollVideo")
@Controller
public class ProviderCollVideoController {


    @Autowired
    ProviderCollVideoPathService providerCollVideoPathService;
    @Autowired
    ProviderCollectionService providerCollectionService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 查看采浆视频
     * @param videoType 视频类型
     * @param collId 采浆ID
     * @return
     */
    @RequestMapping("/getCollVideo")
    public void getCollVideo(String videoType, Long collId, HttpServletResponse response) throws AdscmSystemException {
        collId = 84574L;
        if (AdscmBaseUtils.getInstance().isNotEmpty(videoType,null) && collId != null && collId > 0){
            logger.info("查看【collId:"+collId+" ， videoType:"+videoType+"】采浆视频...");
            Wrapper<ProviderCollVideoPath> wrapper = new EntityWrapper<>();
            wrapper.eq("collId",collId);
            wrapper.eq("videoType",videoType);

            List<ProviderCollVideoPath> paths = providerCollVideoPathService.selectList(wrapper);
            if (paths != null && paths.size()>0){
                ProviderCollVideoPath path = paths.get(0);
                String videoPath = path.getVideoPath();
                if (AdscmBaseUtils.getInstance().isNotEmpty(videoPath,null)){
                    AdscmSystemUtils.getInstance().writeVideoFile(videoPath,response);
                }

            }


        }

    }
    /**
     * 查看采浆视频
     * @param videoType 视频类型
     * @param logId 采浆日志文件名
     * @return
     */
    @RequestMapping("/getCollVideoByLogId")
    public void getCollVideoByLogId(String videoType, String logId, HttpServletResponse response) throws AdscmSystemException {

        //临时用，作为演示用
        if (!AdscmBaseUtils.getInstance().isNotEmpty(logId,null)){
            logId = "temp";
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(videoType,null) && AdscmBaseUtils.getInstance().isNotEmpty(logId,null)){
            logger.info("查看【logId:"+logId+" ， videoType:"+videoType+"】采浆视频...");

            Wrapper<ProviderCollection> collWrapper = new EntityWrapper<>();
            collWrapper.eq("machineLogId",logId);
            List<ProviderCollection> collectionList = providerCollectionService.selectList(collWrapper);
            Long collId = null;
            if (AdscmBaseUtils.getInstance().listIsNotEmpty(collectionList)){
//                collId = collectionList.get(0).getId();
            }
            //临时用，作为演示用
            if(collId == null){
                collId = 84574L;
            }
            this.getCollVideo(videoType,collId,response);

        }

    }

}
